package com.yanqu.road.dao.impl.activity.douluodalu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.DouLuoDaLuEnemyTip;
import com.yanqu.road.entity.activity.douluodalu.DouLuoDaLuTeamLeaveTip;
import com.yanqu.road.entity.activity.douluodalu.DouLuoDaLuUserEvent;
import com.yanqu.road.entity.activity.douluodalu.DouLuoDaLuUserPatrons;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DouLuoDaLuUserDataDaoImpl extends TempDataObjectDao<DouLuoDaLuUserData> {


    public static String getDouLuoDaLuUserDataSql = "select * from `t_c_douluodalu_user` where `activityId`=? and groupId=?";
    public Map<Long, DouLuoDaLuUserData> getDouLuoDaLuUserData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, DouLuoDaLuUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DouLuoDaLuUserData userData = parse(rs);
                map.put(userData.getUserId(), userData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertDouLuoDaLuUserDataSql = "insert into `t_c_douluodalu_user`(activityId,userId,serverId,regionId,openRegionList,energy,energyLastTime,teamId,lastLeaveTime,lastCreateTime,applyJoinTeamMap,event,buffMap,hunHuanMap,allPatronsEarn,patronsMap," +
            "lastUpdateTime,hunLi,totalBlood,blood,score,enemyInfoMap,boxMap,finishEventMap,regionEventMap,userBaseInfo,leaveTip,enemyTipList,fromRegionId,fixedAddition,addition,patronsNum,useEnergy,helpTimes,groupId) " +
            "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRegionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getOpenRegionList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnergyLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastLeaveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastCreateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getApplyJoinTeamMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEvent())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getHunHuanMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAllPatronsEarn()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPatronsMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHunLi()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEnemyInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBoxMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getFinishEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRegionEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getLeaveTipStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEnemyTipList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFromRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFixedAddition()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAddition()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsNum()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuUserDataSql = "update `t_c_douluodalu_user` set serverId=?,regionId=?,openRegionList=?,energy=?,energyLastTime=?,teamId=?,lastLeaveTime=?,lastCreateTime=?,applyJoinTeamMap=?,event=?,buffMap=?,hunHuanMap=?,allPatronsEarn=?,patronsMap=?,lastUpdateTime=?," +
            "hunLi=?,totalBlood=?,blood=?,score=?,enemyInfoMap=?,boxMap=?,finishEventMap=?,regionEventMap=?,userBaseInfo=?,leaveTip=?,enemyTipList=?,fromRegionId=?,fixedAddition=?,addition=?,patronsNum=?,useEnergy=?,helpTimes = ?  where activityId=? and userId=? and groupId=?";

    @Override
    protected boolean updateData(DouLuoDaLuUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRegionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getOpenRegionList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnergyLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastLeaveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastCreateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getApplyJoinTeamMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEvent())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getHunHuanMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAllPatronsEarn()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPatronsMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHunLi()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEnemyInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBoxMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getFinishEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRegionEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getLeaveTipStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEnemyTipList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFromRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFixedAddition()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAddition()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsNum()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuUserData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuUserData douLuoDaLuUserData = new DouLuoDaLuUserData();
        douLuoDaLuUserData.setActivityId(rs.getInt("activityId"));
        douLuoDaLuUserData.setUserId(rs.getLong("userId"));
        douLuoDaLuUserData.setServerId(rs.getLong("serverId"));
        douLuoDaLuUserData.setRegionId(rs.getInt("regionId"));
        douLuoDaLuUserData.setOpenRegionList(JSON.parseObject(rs.getString("openRegionList"), new TypeReference<List<Integer>>() {}));
        douLuoDaLuUserData.setEnergy(rs.getInt("energy"));
        douLuoDaLuUserData.setEnergyLastTime(rs.getLong("energyLastTime"));
        douLuoDaLuUserData.setTeamId(rs.getInt("teamId"));
        douLuoDaLuUserData.setLastLeaveTime(rs.getLong("lastLeaveTime"));
        douLuoDaLuUserData.setLastCreateTime(rs.getLong("lastCreateTime"));
        douLuoDaLuUserData.setApplyJoinTeamMap(JSON.parseObject(rs.getString("applyJoinTeamMap"), new TypeReference<ConcurrentHashMap<Integer, Long>>() {}));
        douLuoDaLuUserData.setEvent(JSON.parseObject(rs.getString("event"), DouLuoDaLuUserEvent.class));
        douLuoDaLuUserData.setBuffMap(JSON.parseObject(rs.getString("buffMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        douLuoDaLuUserData.setHunHuanMap(JSON.parseObject(rs.getString("hunHuanMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        douLuoDaLuUserData.setAllPatronsEarn(rs.getLong("allPatronsEarn"));
        douLuoDaLuUserData.setPatronsMap(JSON.parseObject(rs.getString("patronsMap"), new TypeReference<ConcurrentHashMap<Integer, DouLuoDaLuUserPatrons>>() {}));
        douLuoDaLuUserData.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        douLuoDaLuUserData.setHunLi(rs.getLong("hunLi"));
        douLuoDaLuUserData.setTotalBlood(rs.getLong("totalBlood"));
        douLuoDaLuUserData.setBlood(rs.getLong("blood"));
        douLuoDaLuUserData.setScore(rs.getLong("score"));
        douLuoDaLuUserData.setEnemyInfoMap(JSON.parseObject(rs.getString("enemyInfoMap"), new TypeReference<ConcurrentHashMap<Long, Integer>>() {}));
        douLuoDaLuUserData.setBoxMap(JSON.parseObject(rs.getString("boxMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        douLuoDaLuUserData.setFinishEventMap(JSON.parseObject(rs.getString("finishEventMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        String str = rs.getString("regionEventMap");
        if(!StringUtils.isNullOrEmpty(str)) {
            douLuoDaLuUserData.setRegionEventMap(JSON.parseObject(str, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
            }));
        }
        douLuoDaLuUserData.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        douLuoDaLuUserData.setLeaveTip(JSON.parseObject(rs.getString("leaveTip"), DouLuoDaLuTeamLeaveTip.class));
        douLuoDaLuUserData.setEnemyTipList(JSON.parseObject(rs.getString("enemyTipList"), new TypeReference<List<DouLuoDaLuEnemyTip>>() {}));
        douLuoDaLuUserData.setFromRegionId(rs.getInt("fromRegionId"));
        douLuoDaLuUserData.setFixedAddition(rs.getInt("fixedAddition"));
        douLuoDaLuUserData.setAddition(rs.getInt("addition"));
        douLuoDaLuUserData.setPatronsNum(rs.getInt("patronsNum"));
        douLuoDaLuUserData.setUseEnergy(rs.getInt("useEnergy"));
        douLuoDaLuUserData.setHelpTimes(rs.getInt("helpTimes"));
        douLuoDaLuUserData.setGroupId(rs.getInt("groupId"));
        return douLuoDaLuUserData;
    }
}
